package gdut.imis.controller;

import gdut.imis.Utils.Result;
import gdut.imis.domain.FileType;
import gdut.imis.domain.FileWritePermissionApplication;
import gdut.imis.service.FileWritePermissionApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 22:36
 */
@RestController
@RequestMapping("/file-write-permission-application")
@Api(value = "档案录入权限申请接口", tags = {"档案录入权限申请接口"})
@RequiredArgsConstructor
public class FileWritePermissionApplicationController extends BaseController {

    private final FileWritePermissionApplicationService fileWritePermissionApplicationService;

    @ApiOperation("用户申请档案录入权限")
    @PostMapping("/applyFileWritePermission")
    public Result applyFileWritePermission(MultipartFile file, FileType fileType) throws IOException {
        return fileWritePermissionApplicationService.applyFileWritePermission(file, fileType);
    }

    @ApiOperation("用户查询档案录入权限申请情况")
    @GetMapping("/getFileWritePermissionApplyResult")
    public Result getFileWritePermissionApplyResult() {
        return fileWritePermissionApplicationService.getFileWritePermissionApplyResult();
    }

    @ApiOperation("根据学校id查询未处理的档案录入权限申请(三级管理员才有的权限)")
    @GetMapping("/getPageFileWritePersissionApplication/{universityId}/{pageNo}/{pageSize}")
    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public Result getPageFileWritePersission(
            @PathVariable("universityId") Long universityId,
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize) {
        Result result = this.checkPageParams(pageNo, pageSize);
        if (result.getCode() == 0) {
            return fileWritePermissionApplicationService.getPageFileWritePersission(universityId, pageNo, pageSize);
        }
        return result;
    }

    @ApiOperation("处理档案录入权限申请（三级管理员才有的权限）")
    @PostMapping("/handleFileWritePermissionApply")
    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public Result handleFileWritePermissionApply(@RequestBody FileWritePermissionApplication fileWritePermissionApplication) {
        return fileWritePermissionApplicationService.handleFileWritePermissionApply(fileWritePermissionApplication);
    }
}
